package net.maku.kyy.controller;

import cn.hutool.http.HttpUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.framework.operatelog.annotations.OperateLog;
import net.maku.framework.operatelog.enums.OperateTypeEnum;
import net.maku.kyy.entity.KyyGameEntity;
import net.maku.kyy.service.KyyGameService;
import net.maku.kyy.query.KyyGameQuery;
import net.maku.kyy.vo.KyyGameVO;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 快应用
 *
 * @author MAKU babamu@126.com
 * <a href="https://maku.net">MAKU</a>
 */
@RestController
@RequestMapping("/kyy/game")
@Tag(name="快应用")
@AllArgsConstructor
public class KyyGameController {
    private final KyyGameService kyyGameService;

    @GetMapping("page")
    @Operation(summary = "分页")
    public Result<PageResult<KyyGameVO>> page(@ParameterObject @Valid KyyGameQuery query){
        PageResult<KyyGameVO> page = kyyGameService.page(query);
        return Result.ok(page);
    }


    @GetMapping("getCategory2Name")
    @Operation(summary = "获取2级标签")
    public Result<List<Map<String, Object>>> getCategory2Name(){
        List<Map<String, Object>> category2Name = kyyGameService.getCategory2Name();
        return Result.ok(category2Name);
    }


    @GetMapping("getCategory3Name")
    @Operation(summary = "获取3级标签")
    public Result<List<Map<String, Object>>> getCategory3Name(KyyGameQuery query){
        List<Map<String, Object>> category3Name = kyyGameService.getCategory3Name(query);
        return Result.ok(category3Name);
    }

    @PostMapping("addClick")
    @Operation(summary = "增加点击次数")
    @PreAuthorize("hasAuthority('kyy:user')")
    public Result<String> addClick(@RequestBody @Valid KyyGameVO vo){
        KyyGameEntity kyyGameEntity = kyyGameService.getById(vo.getId());
        if(kyyGameEntity!=null){
            KyyGameVO setData =new KyyGameVO();
            setData.setId(vo.getId());
            setData.setClickCount(kyyGameEntity.getClickCount()+1);
            kyyGameService.update(setData);
        }
        return Result.ok();
    }
    @PostMapping("addOpen")
    @Operation(summary = "增加打开次数")
    @PreAuthorize("hasAuthority('kyy:user')")
    public Result<String> addOpen(@RequestBody @Valid KyyGameVO vo){
        KyyGameEntity kyyGameEntity = kyyGameService.getById(vo.getId());
        if(kyyGameEntity!=null){
            KyyGameVO setData =new KyyGameVO();
            setData.setId(vo.getId());
            setData.setOpenCount(kyyGameEntity.getOpenCount()+1);
            kyyGameService.update(setData);
        }
        return Result.ok();
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    public Result<KyyGameVO> get(@PathVariable("id") Long id){
        KyyGameVO data = kyyGameService.get(id);

        return Result.ok(data);
    }

    @PostMapping
    @Operation(summary = "保存")
    @OperateLog(type = OperateTypeEnum.INSERT)
    @PreAuthorize("hasAuthority('kyy:game')")
    public Result<String> save(@RequestBody KyyGameVO vo){
        kyyGameService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @OperateLog(type = OperateTypeEnum.UPDATE)
    @PreAuthorize("hasAuthority('kyy:game')")
    public Result<String> update(@RequestBody @Valid KyyGameVO vo){
        kyyGameService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @OperateLog(type = OperateTypeEnum.DELETE)
    @PreAuthorize("hasAuthority('kyy:game')")
    public Result<String> delete(@RequestBody List<Long> idList){
        kyyGameService.delete(idList);

        return Result.ok();
    }


    @GetMapping("export")
    @Operation(summary = "导出")
    @OperateLog(type = OperateTypeEnum.EXPORT)
    @PreAuthorize("hasAuthority('kyy:game')")
    public void export() {
        kyyGameService.export();
    }

}